package yl.hs.bmipfull.service.salemanage.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.salemanage.ICustomerAccessPlanDao;
import yl.hs.bmipfull.pojo.salemanage.CustomerAccessPlanEntity;
import yl.hs.bmipfull.service.salemanage.ICustomerAccessPlanService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.datastate.RelationType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * 作用:  客户拜访计划服务层接口实现<br/>
 * 创建时间: 2021年05月19日 18时58分11秒 <br/>
 *
 * @author 谢东
 */
@Service
public class CustomerAccessPlanServiceImpl extends SaleBaseService implements ICustomerAccessPlanService {

    @Autowired
    ICustomerAccessPlanDao dao;

    @Override
    public List<CustomerAccessPlanEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(CustomerAccessPlanEntity.class);
        return dao.queryList(pageQuery.getQuery());
    }

    @Override
    public PageResult<CustomerAccessPlanEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(CustomerAccessPlanEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<CustomerAccessPlanEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public CustomerAccessPlanEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Transactional
    public Integer saveCustomerAccessPlan(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;

        if (Tl.isEmpty(sn)) {
            //执行保存
            CustomerAccessPlanEntity entity = new CustomerAccessPlanEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(CustomerAccessPlanEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                var shareCode = writeUserTag(entity.getSn(), entity.getClass(), RelationType.CREATE.toString());
                saveGS(
                        entity.getSn(),
                        entity.getLinkers(),
                        String.join(",",
                                entity.getLinkers(),
                                entity.getMainPurpose(),
                                entity.getSecondaryPurpose(),
                                entity.getTeamUsers()
                        ),
                        entity,
                        HsApp.getCurrent() != null ? HsApp.getCurrent().getUserSN() : null,
                        shareCode
                );

                writeLog(ActionType.CREATE, entity.getSn(), "插入客户拜访计划信息", Tl.toJson(entity), CustomerAccessPlanEntity.class);
            }
        } else {
            //执行修改
            var entity = findBySn(sn);
            var backJson = entity.toJson();
            var changeLog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, changeLog, backJson, CustomerAccessPlanEntity.class);
            }
        }
        return result;
    }

    @Transactional
    public Integer delete(String[] sn) {

        return 0;
    }
}